package com.example.jack.community.view.dynamic.dynamic_bean;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jack.community.R;

import java.util.List;

/**
 * Created by DP on 2017/12/6.
 */

public class ContentRecyclerViewAdapter extends RecyclerView.Adapter<ContentRecyclerViewAdapter.ViewHolder>{

    private List<ContentData> mCdata;
    private Context mContext;
    private LayoutInflater inflater;

    @Override
    public void onBindViewHolder(final ContentRecyclerViewAdapter.ViewHolder holder, int position) {
        ContentData data = mCdata.get(position);
//        Glide.with(mContext).load(data.getCuserhead()).into(holder.poster);//在设配器器图片请求
        holder.Cuserhead.setImageResource(data.getCuserhead());
        holder.Cusername.setText(data.getCusername());
        holder.Cpublicationtime.setText(data.getCpublicationtime());
        holder.Cfabulousnum.setText(data.getCfabulousnum());
        holder.Cusercontent.setText(data.getCusercontent());

        //3.在onBindViewHolder方法内，实现回调
        //判断是否设置了监听器
        if (mOnItemClickListener != null) {
            //为ItemView设置监听器
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition(); // 1
                    mOnItemClickListener.onItemClick(holder.itemView, position); // 2
                }
            });
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.fragment_dynamic_comment_recyclearview, parent, false);
//        //注册点击事件
        final ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView Cuserhead;
        TextView Cusername;
        TextView Cpublicationtime;
        TextView Cfabulousnum;
        TextView Cusercontent;
        View activityView;

        public ViewHolder(View itemView) {
            super(itemView);
            Cuserhead = (ImageView) itemView.findViewById(R.id.iv_activity_comment_userhead);
            activityView = itemView;
            Cpublicationtime = (TextView) itemView.findViewById(R.id.tv_activity_comment_publicationtime);
            Cusername = (TextView) itemView.findViewById(R.id.tv_activity_comment_username);
            Cfabulousnum = (TextView) itemView.findViewById(R.id.tv_activity_comment_fabulousnum);
            Cusercontent = (TextView) itemView.findViewById(R.id.tv_activity_comment_usercontent);
        }
    }

    @Override
    public int getItemCount() {
        return mCdata.size();
    }

    //重写适配器的变量 Context mContext为了拿到生命周期
    public ContentRecyclerViewAdapter(Context mContext, List<ContentData> mCdata) {
        this.mContext = mContext;
        this.mCdata = mCdata;
        inflater = LayoutInflater.from(mContext);
    }

    public ContentRecyclerViewAdapter(List<ContentData> dataList) {
        this.mCdata = dataList;
        Log.d( "dataSize", dataList.size() + "" );
    }



    //实现RecyclerView点击事件

    //1.新建点击内部接口
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    //2.新建私有变量用于保存用户设置的监听器及其set方法
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
